package com.walleipt.scala

import scala.math._;
import java.text.Normalizer.Form

object Demo {

  def main(args: Array[String]): Unit = {
    /* 定义变量 */
    var var1 = 1; var1 = 2
    println(var1);

    /* 定义变量 */
    var var2: String = "我是一个字符串";
    var2 = "我真的是个字符串"
    println(var2);

    /* val定义表示常量不能改变 */
    val val1: String = "val-不能改变"
    println(val1);

    /* 一行定义多个变量 */
    val xmax, ymax = 100
    var greeting, message: String = "111"; // 此定义表示greeting,message的初始值都是111
    println(greeting + "-" + message);

    /* Byte, Char, Short, Int, Long, Float, Double, Boolean */

    /* 算术和操作符重载 */
    val answer = 8 * 5 + 2
    println(answer)

    val a = 5; var b = 10;
    println("a.+(b):" + a.+(b))

    println(1.to(9));

    sqrt(2);

    val result1 = if (1 == 1) 2 else 0
    println("分支最后返回2或0->" + result1)

    /* 块结构输出  */
    val result2 = {
      val a = 6;
      a * 6 + 1
    }
    println(result2)

    /* 遍历1-10 */
    for (i <- 1 to 10) //  for( i <- (1 to 10) ) 
    {
      print(i + ",")
    }
    println()

    val hi = "hello word"
    for (i <- (0 to hi.length())) {
      print(i + ",")
    }
    println() 
    
    
    /* 类似矩阵 */
    for (i <- 1 to 3; j <- 1 to 4) {
      print(10*i+j+",")
    }
   
    println() 
    for ( i<- 1 to 3; j <- 1 to 4; if i!=j) print( 10*i+j+",")
    println() 
    
    println( sum( 1, 2 ) ) 
    println( sum( 1 to 12:_* ) ) // 讲1 to 2转成序列类型
    
  }
  
  def sum( n: Int*) = {
    var result = 0  
    for ( i <- n ) result += i
    result ;
  }
  
  
}